<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区ID" prop="communityId">
        <el-input
          v-model="queryParams.communityId"
          placeholder="请输入小区ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理人ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入代理人ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否近地铁" prop="isNear">
        <el-input
          v-model="queryParams.isNear"
          placeholder="请输入是否近地铁"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
    
     
     
     
      <el-form-item label="车位编号" prop="parkingNumber">
        <el-input
          v-model="queryParams.parkingNumber"
          placeholder="请输入车位编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否推荐" prop="isTop">
        <el-input
          v-model="queryParams.isTop"
          placeholder="请输入是否推荐"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:manage_parking:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:manage_parking:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:manage_parking:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:manage_parking:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manage_parkingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="parkingId" />
      <el-table-column label="小区ID" align="center" prop="communityId" />
      <el-table-column label="代理人ID" align="center" prop="userId" />
      <el-table-column label="是否近地铁" align="center" prop="isNear" />
      <el-table-column label="总价" align="center" prop="allPrice" />
      <el-table-column label="月租" align="center" prop="monthPrice" />
      <el-table-column label="楼栋ID" align="center" prop="buildingId" />
      <el-table-column label="主图" align="center" prop="coverImg">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.coverImg[0]"
            :zoom-rate="1.2"
            :max-scale="7"
            preview-teleported
            :min-scale="0.2"
            :preview-src-list="scope.row.coverImg"
            show-progress
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="详情图" align="center" prop="descImgs">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.descImgs[0]"
            :zoom-rate="1.2"
            preview-teleported
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="scope.row.descImgs"
            show-progress
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center" prop="videoUrl" />
      <el-table-column label="标签" align="center" prop="tags" />
      <el-table-column label="详情" align="center" prop="detailContent" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态0停用1启用" align="center" prop="status" />
      <el-table-column label="车位编号" align="center" prop="parkingNumber" />
      <el-table-column label="是否推荐" align="center" prop="isTop" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:manage_parking:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:manage_parking:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车位管理对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="manage_parkingRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属" prop="communityId">
              <el-input v-model="form.communityId" placeholder="请输入小区ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理人" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入代理人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="楼栋" prop="buildingId">
              <el-input v-model="form.buildingId" placeholder="楼栋" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="近电梯" prop="isNear">
              <el-input v-model="form.isNear" placeholder="是否近电梯" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="总价" prop="allPrice">
              <el-input v-model="form.allPrice" placeholder="请输入总价" />
            </el-form-item>
            
          </el-col>
          <el-col :span="12">
            <el-form-item label="月租" prop="monthPrice">
              <el-input v-model="form.monthPrice" placeholder="请输入月租" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="车位编号" prop="parkingNumber">
              <el-input v-model="form.parkingNumber" placeholder="请输入车位编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否推荐" prop="isTop">
              <el-input v-model="form.isTop" placeholder="请输入是否推荐" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标签" prop="tags">
              <el-input v-model="form.tags" placeholder="请输入标签" />
            </el-form-item>
          </el-col>
        </el-row>
       
       
        
       
        <el-form-item label="主图" prop="coverImg">
          <!-- <el-input v-model="form.coverImg" placeholder="请输入主图" /> -->
           <el-upload
           :limit="1" 
            :headers="upload.headers" 
            :action="upload.url"
            v-model:file-list="fileCoverList"
            :on-success="(response, file, fileList)=>handleFileSuccess(response, file, fileList, 1)"
            list-type="picture-card"
          >
          <el-icon><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="详情图" prop="descImgs">
          <!-- <el-input v-model="form.descImgs" placeholder="请输入详情图" /> -->
          <el-upload
           :limit="5" 
            :headers="upload.headers" 
            :action="upload.url"
            v-model:file-list="fileList"
           
            :on-success="(response, file, fileList)=>handleFileSuccess(response, file, fileList, 2)"
            list-type="picture-card"
          >
          <el-icon><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="视频" prop="videoUrl">
          <el-input v-model="form.videoUrl" placeholder="请输入视频" />
        </el-form-item>
        
        <el-form-item label="详情">
          <editor v-model="form.detailContent" :min-height="192"/>
        </el-form-item> 
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Manage_parking">
import { listManage_parking, getManage_parking, delManage_parking, addManage_parking, updateManage_parking } from "@/api/manage/manage_parking";
import { getToken } from "@/utils/auth";
const { proxy } = getCurrentInstance();
const fileList=ref([])
let fileCoverList=ref([])
const manage_parkingList = ref([]);
const manageCommunityList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedManageCommunity = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    communityId: null,
    userId: null,
    isNear: null,
    allPrice: null,
    monthPrice: null,
    buildingId: null,
    coverImg: null,
    descImgs: null,
    videoUrl: null,
    tags: null,
    detailContent: null,
    sort: null,
    status: null,
    parkingNumber: null,
    isTop: null
  },
  rules: {
    status: [
      { required: true, message: "状态0停用1启用不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/*** 图片上传参数 */
const upload = reactive({
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/common/upload"
});


/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true;
};

/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList,type) => {
  switch(type){
    case 1:
      form.value.coverImg=response.fileName
    case 2:
      

      form.value.descImgs=fileList.map(item=>{
        return item.response?item.response.fileName:item.name.split(import.meta.env.VITE_APP_BASE_API)[1]
      }).filter(item=>{return item}).join(',')

  }
};


/** 查询车位管理列表 */
function getList() {
  loading.value = true;
  listManage_parking(queryParams.value).then(response => {
    manage_parkingList.value = response.rows.map(item=>{
      item.coverImg=item.coverImg.split(',').map(img=>{
        img=import.meta.env.VITE_APP_BASE_API+img;
        return img
      })
      item.descImgs=item.descImgs.split(',').map(img=>{
        img=import.meta.env.VITE_APP_BASE_API+img;
        return img
      })
      return item
    })
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    parkingId: null,
    communityId: null,
    userId: null,
    isNear: null,
    allPrice: null,
    monthPrice: null,
    buildingId: null,
    coverImg: null,
    descImgs: null,
    videoUrl: null,
    tags: null,
    detailContent: null,
    sort: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    parkingNumber: null,
    isTop: null
  };
  manageCommunityList.value = [];
  proxy.resetForm("manage_parkingRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.parkingId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加车位管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _parkingId = row.parkingId || ids.value
  getManage_parking(_parkingId).then(response => {
    form.value = response.data;



     fileCoverList.value=response.data.coverImg.split(',').map(img=>{
        img=import.meta.env.VITE_APP_BASE_API+img;
        return {
          uid:img,
          url:img,
          name:img
        }
      })
      fileList.value=response.data.descImgs.split(',').map(img=>{
        img=import.meta.env.VITE_APP_BASE_API+img;
         return {
          uid:img,
          url:img,
          name:img
        }
      })




    manageCommunityList.value = response.data.manageCommunityList;
    open.value = true;
    title.value = "修改车位管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["manage_parkingRef"].validate(valid => {
    if (valid) {
      form.value.manageCommunityList = manageCommunityList.value;
      if (form.value.parkingId != null) {
        updateManage_parking(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManage_parking(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _parkingIds = row.parkingId || ids.value;
  proxy.$modal.confirm('是否确认删除车位管理编号为"' + _parkingIds + '"的数据项？').then(function() {
    return delManage_parking(_parkingIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 小区信息序号 */
function rowManageCommunityIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 小区信息添加按钮操作 */
function handleAddManageCommunity() {
  let obj = {};
  obj.province = "";
  obj.city = "";
  obj.developerName = "";
  obj.longitude = "";
  obj.latitude = "";
  obj.address = "";
  obj.remark = "";
  manageCommunityList.value.push(obj);
}

/** 小区信息删除按钮操作 */
function handleDeleteManageCommunity() {
  if (checkedManageCommunity.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的小区信息数据");
  } else {
    const manageCommunitys = manageCommunityList.value;
    const checkedManageCommunitys = checkedManageCommunity.value;
    manageCommunityList.value = manageCommunitys.filter(function(item) {
      return checkedManageCommunitys.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleManageCommunitySelectionChange(selection) {
  checkedManageCommunity.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/manage_parking/export', {
    ...queryParams.value
  }, `manage_parking_${new Date().getTime()}.xlsx`)
}

getList();
</script>
